package com.project.forlearningdemo.service.impl;

import com.alibaba.fastjson.JSON;
import com.project.forlearningdemo.constant.ResponseTool;
import com.project.forlearningdemo.dao.ResourceMapper;
import com.project.forlearningdemo.pojo.Resource;
import com.project.forlearningdemo.pojo.vo.Condition;
import com.project.forlearningdemo.pojo.vo.Info;
import com.project.forlearningdemo.pojo.vo.ResponseInfo;
import com.project.forlearningdemo.service.ResourceService;
import com.project.forlearningdemo.utils.PageInfoUtil;
import com.project.forlearningdemo.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    ResourceMapper resourceMapper;
    @Autowired
    PageInfoUtil<Resource> pageInfoUtil;

    Info<Resource> resourceInfo = new Info<>();

    /**
     *  搜索资源
     */
    @Override
    public String getResource(String condition){
        //转换
        Condition condition1 = (Condition) StringUtils.tranJSON(condition, new Condition());
        if(condition1.getPageNum() == null) return JSON.toJSONString(new ResponseInfo<>(500, "pageNum（当前页数）不能为空", false));
        //搜索
        List<Resource> resources = resourceMapper.queryResource(condition1.getResourceId(), condition1.getUserId(), condition1.getStr(), StringUtils.makeLabels(condition1.getLabels()), condition1.getStatus(), condition1.getIsOr(), condition1.getOrderBy(), condition1.getIsDesc(), condition1.getRandom());

        //临时存储
        List<Resource> resources1 = null;
        //标志 判断是否为精选标签
        int flag = 0;
        if(condition1.getIsS()!=null && condition1.getIsS()==true) {
            flag = 1;
            resources1 = new ArrayList<>();
        }

        //添加动态标签
        for(Resource resource : resources){
            if("通过".equals(resource.getStatus())){
                //时间差
                Long ctime = (new Date().getTime()) - resource.getFTime().getTime();
                if( ctime <  864000000L){
                    //离现在比较近, 加上标签
                    resource.setLabels("最新;" + resource.getLabels());
                }
                //精选
                int num = resource.getCollectionNum();
                int scanNum = resource.getScanNum();
                if( num >= 2 || scanNum >= 100){
                    //若收藏量不小于2，则添加精选标签
                    resource.setLabels("精选;"+ resource.getLabels());
                    //判断是否只看精选
                    if(flag == 1){
                        resources1.add(resource);
                    }
                }
            }
        }
        //将获取的资源集给他
        List<Resource> resources2 = flag==1 ? resources1 : resources;

        //设置分页
        resourceInfo = pageInfoUtil.resetPage(condition1.getPageNum(), PageInfoUtil.LIMITPAGE, resources2, resourceInfo);

        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_SUCCESS.code, ResponseTool.QUERY_SUCCESS.msg, resourceInfo));
    }

    /**
     * 删除资源
     */
    @Override
    public String deleteResource(String condition) {
        //条件
        Condition condition1 = (Condition) StringUtils.tranJSON(condition, new Condition());
        if(resourceMapper.deleteResource(condition1.getResourceId(), condition1.getUserId(), condition1.getStatus())){
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DELETE_SUCCESS.code, ResponseTool.DELETE_SUCCESS.msg, true));
        }
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DELETE_FAIL.code, ResponseTool.DELETE_FAIL.msg, false));
    }

    /**
     * 增加资源
     */
    @Override
    public String addResource(String resource) {
        //资源
        Resource resource1 = (Resource)StringUtils.tranJSON(resource, new Resource());
        if(resource1.getStatus() == null) resource1.setStatus("审核中");
        if(resourceMapper.addResource(resource1)) {
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.ADD_SUCCESS.code, ResponseTool.ADD_SUCCESS.msg, resource1));
        }
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.ADD_FAIL.code, ResponseTool.ADD_FAIL.msg, false));
    }

    /**
     * 更新资源
     */
    @Override
    public String updateResource(String resource) {
        //资源
        Resource resource1 = (Resource) StringUtils.tranJSON(resource , new Resource());
        //检查
        Integer resourceId = resource1.getResourceId();
        if(resourceId == null || resourceId <= 0) return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DATA_ERROR.code, ResponseTool.DATA_ERROR.msg, false));
        //执行操作
        if(resourceMapper.updateResource(resource1)){
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.UPDATE_SUCCESS.code, ResponseTool.UPDATE_SUCCESS.msg, resource1));
        }
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.UPDATE_FAIL.code, ResponseTool.UPDATE_FAIL.msg, false));
    }

    /**
     * 增加收藏
     */
    @Override
    public synchronized String addCollection(int resourceId, int userId) {
        //参数校验
        if(resourceId<=0 || userId<=0) return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DATA_ERROR.code, ResponseTool.DATA_ERROR.msg, false));
        if(resourceMapper.queryCollection(resourceId, userId, null).size()!=0) return JSON.toJSONString(new ResponseInfo<>(500, "已经收藏过该资源，无法再次收藏", false));

        //增加收藏品
        if(resourceMapper.addCollection(resourceId, userId)){
            //增加收藏量
            resourceMapper.addScanNumOrCollection(resourceId, "collectionNum");
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.ADD_SUCCESS.code, ResponseTool.ADD_SUCCESS.msg, true));
        }

        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.ADD_FAIL.code, ResponseTool.ADD_FAIL.msg, false));
    }

    /**
     * 删除收藏
     */
    @Override
    public String deleteCollection(int resourceId, int userId) {
        //删除收藏品
        if (resourceMapper.deleteCollection(resourceId, userId))
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DELETE_SUCCESS.code, ResponseTool.DELETE_SUCCESS.msg, true));
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DELETE_FAIL.code, ResponseTool.DELETE_FAIL.msg, false));
    }

    /**
     * 查询收藏
     */
    @Override
    public String queryCollection(String collection) {
        //转换
        Condition condition = (Condition) StringUtils.tranJSON(collection, new Condition());
        if(condition.getPageNum() == null) return JSON.toJSONString(new ResponseInfo<>(500, "pageNum（当前页数）不能为空", false));
        //查询
        List<Resource> resources = resourceMapper.queryCollection(condition.getResourceId(), condition.getUserId(), StringUtils.makeLabels(condition.getLabels()));
        //分页
        resourceInfo = pageInfoUtil.resetPage(condition.getPageNum(), PageInfoUtil.LIMITPAGE, resources, resourceInfo);
        //返回结果
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_SUCCESS.code, ResponseTool.QUERY_SUCCESS.msg, resourceInfo));
    }

    /**
     * 增加浏览量
     */
    @Override
    public synchronized String addScanNum(int resourceId) {
        resourceMapper.addScanNumOrCollection(resourceId, "scanNum");
        return JSON.toJSONString(new ResponseInfo<>(200, "增加成功", true));
    }


}
